package com.wisemoresystem.seal.service;

import java.sql.Timestamp;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.model.t_mac_move_log;
import com.wisemoresystem.seal.util.SpringTools;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class MacMoveLogService extends BaseService {

	// 批量保存mac
	@Transactional
	public void saveMacs(String[] macCodes, String note) {

		for (String code : macCodes) {
			t_mac mac = new t_mac();
			mac.setCode(code);
			mac.setMac(code);
			mac.setNote(note);
			mac.setFlag("停用");
			mac.setFlag_stop("在库");
			mac.setUser_id(WebTools.getCurrentUserId());
			mac.setBank_id(WebTools.getCurrentBankId());
			mac.setUser_bank_id(WebTools.getCurrentBankId());

			ht.save(mac);

			t_mac_move_log log = new t_mac_move_log();
			log.setMac_id(mac.get_id());
			log.setType("录入");
			log.setNote(note);
			log.setUser_id(mac.getUser_id());
			log.setBank_id(mac.getBank_id());
			log.setIs_verify("否");
			log.setIs_end("是");

			ht.save(log);
		}

	}

	// 印控仪分发
	@Transactional
	public void push(String mac_id, String dest_bank_id, String note) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);
		t_bank destBank = super.findObjById(dest_bank_id, t_bank.class);

		mac.setFlag("停用");
		mac.setFlag_stop("分发中");

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("分发");
		log.setNote(note);
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(destBank.get_id());
		log.setUser_id(WebTools.getCurrentUserId());
		log.setIs_verify("否");
		log.setIs_end("否");

		ht.update(mac);
		ht.save(log);

	}

	// 查询分发中的日志
	public t_mac_move_log findPushingMacLog(String mac_id) {
		String hql = "from t_mac_move_log t where t.mac_id=? and t.status=0 and t.type='分发' and t.is_end='否'";
		t_mac_move_log log = super.findObj(hql, mac_id);
		return log;
	}

	// 印控仪 接收
	@Transactional
	public void recv(String mac_id, String note) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);

		mac.setFlag("停用");
		mac.setFlag_stop("在库");
		mac.setBank_id(WebTools.getCurrentBankId());

		t_mac_move_log pushingLog = findPushingMacLog(mac.get_id());
		pushingLog.setIs_end("是");

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("接收");
		log.setNote(note);
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(WebTools.getCurrentBankId());
		log.setUser_id(WebTools.getCurrentUserId());
		log.setIs_verify("否");
		log.setIs_end("是");

		ht.update(mac);
		ht.update(pushingLog);
		ht.save(log);
	}

	// 印控仪 启用
	@Transactional
	public boolean start(String mac_id) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("在库"))) {
			return false;
		}

		mac.setFlag("启用");
		mac.setFlag_stop("在库");
		mac.setBank_id(WebTools.getCurrentBankId());

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("启用");
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(WebTools.getCurrentBankId());
		log.setUser_id(WebTools.getCurrentUserId());
		log.setIs_verify("否");
		log.setIs_end("是");

		ht.update(mac);
		ht.save(log);

		return true;
	}

	// 印控仪 停用
	@Transactional
	public boolean stop(String mac_id, String flag_stop) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);

		if (!"启用".equals(mac.getFlag())) {
			return false;
		}
		// 停用之后，卸载所有印章......

		mac.setFlag("停用");
		mac.setFlag_stop(flag_stop);
		mac.setBank_id(WebTools.getCurrentBankId());

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("停用");
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(WebTools.getCurrentBankId());
		log.setUser_id(WebTools.getCurrentUserId());
		log.setIs_verify("否");
		log.setIs_end("是");

		ht.update(mac);
		ht.save(log);

		return true;
	}

	// 印控仪 报废
	@Transactional
	public boolean destory(String mac_id) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("在库"))) {
			return false;
		}

		mac.setFlag("报废");
		mac.setFlag_stop("在库");
		mac.setBank_id(WebTools.getCurrentBankId());

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("报废");
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(WebTools.getCurrentBankId());
		log.setUser_id(WebTools.getCurrentUserId());
		log.setIs_end("是");

		ht.update(mac);
		ht.save(log);

		return true;
	}

	// 印控仪 调拨申请
	@Transactional
	public boolean transfer_apply(String mac_id, String dest_bank_id,
			String note) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);
		t_bank bank = super.findObjById(mac.getBank_id(), t_bank.class);
		t_bank destBank = super.findObjById(dest_bank_id, t_bank.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("在库"))) {
			return false;
		}

		// mac.setFlag("停用");
		mac.setFlag_stop("调拨申请中");
		mac.setBank_id(WebTools.getCurrentBankId());

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("调拨");
		log.setNote(note);
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(destBank.get_id());
		log.setUser_id(WebTools.getCurrentUserId());

		log.setIs_verify("是");
		log.setVerify_flag("审核中");
		log.setVerify_bank_id(bank.getSuper_id());// 上级行审核
		// log.setVerify_bank_id(bank.get_id());// 方便测试用
		log.setIs_end("否");

		ht.update(mac);
		ht.save(log);

		return true;
	}

	// 查询 调拨审核中 的日志
	public t_mac_move_log findTransferVerifyingLog(String mac_id) {

		String hql = "from t_mac_move_log t where t.status=0 and t.mac_id=? and t.type='调拨' and t.verify_flag='审核中' and t.verify_bank_id=?";
		t_mac_move_log log = (t_mac_move_log) super.findObj(hql, mac_id,
				WebTools.getCurrentBankId());

		return log;
	}

	// 调拨确认
	@Transactional
	public boolean transfer_verify(String mac_id, String verify_flag,
			String note) {

		t_mac mac = super.findObjById(mac_id, t_mac.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("调拨申请中"))) {
			return false;
		}
		t_mac_move_log log = this.findTransferVerifyingLog(mac.get_id());

		if ("不通过".equals(verify_flag)) {
			mac.setFlag("停用");
			mac.setFlag_stop("在库");

			log.setVerify_flag("不通过");
			log.setIs_end("是");
		} else if ("通过".equals(verify_flag)) {
			mac.setFlag_stop("调拨中");

			log.setVerify_flag("通过");
			log.setIs_end("否");
		}

		log.setVerify_note(note);

		log.setVerify_bank_id(WebTools.getCurrentBankId());
		log.setVerify_user_id(WebTools.getCurrentUserId());
		log.setVerify_time(new Timestamp(System.currentTimeMillis()));

		ht.update(mac);
		ht.update(log);

		return true;

	}

	// 查询调拨审批后的 日志
	public t_mac_move_log findTransferVerifyedLog(String mac_id) {
		String hql = "from t_mac_move_log t where t.status=0 and t.mac_id=? and t.type='调拨' and t.verify_flag='通过' and t.is_end='否' and t.bank_id_dest=?";
		t_mac_move_log log = (t_mac_move_log) super.findObj(hql, mac_id,
				WebTools.getCurrentBankId());

		return log;
	}

	// 调拨 接收
	@Transactional
	public boolean transfer_recv(String mac_id, String note) {

		t_mac mac = super.findObjById(mac_id, t_mac.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("调拨中"))) {
			return false;
		}

		mac.setFlag("停用");
		mac.setFlag_stop("在库");
		mac.setBank_id(WebTools.getCurrentBankId());

		t_mac_move_log transferVerifyedLog = findTransferVerifyedLog(mac
				.get_id());
		transferVerifyedLog.setIs_end("是");

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("调拨接收");
		log.setNote(note);
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(WebTools.getCurrentBankId());
		log.setUser_id(WebTools.getCurrentUserId());
		log.setIs_verify("否");
		log.setIs_end("是");

		ht.update(mac);
		ht.update(transferVerifyedLog);
		ht.save(log);

		return true;
	}

	// 上缴 申请
	@Transactional
	public boolean over_apply(String mac_id, String note) {
		t_mac mac = super.findObjById(mac_id, t_mac.class);
		t_bank bank = super.findObjById(mac.getBank_id(), t_bank.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("上缴停用"))) {
			return false;
		}

		// mac.setFlag("停用");
		mac.setFlag_stop("上缴申请中");

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("上缴");
		log.setNote(note);
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(bank.getSuper_id());
		log.setUser_id(WebTools.getCurrentUserId());

		log.setIs_verify("是");
		log.setVerify_flag("审核中");
		log.setVerify_bank_id(bank.getSuper_id());// 上级行审核
		// log.setVerify_bank_id(bank.get_id());// 方便测试用
		log.setIs_end("否");

		ht.update(mac);
		ht.save(log);

		return true;
	}

	// 查询 调拨审核中 的日志
	public t_mac_move_log findOverVerifyingLog(String mac_id) {

		String hql = "from t_mac_move_log t where t.status=0 and t.mac_id=? and t.type='上缴' and t.verify_flag='审核中' and t.verify_bank_id=?";
		t_mac_move_log log = (t_mac_move_log) super.findObj(hql, mac_id,
				WebTools.getCurrentBankId());

		return log;
	}

	// 上缴 审批
	@Transactional
	public boolean over_verify(String mac_id, String verify_flag, String note) {

		t_mac mac = super.findObjById(mac_id, t_mac.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("上缴申请中"))) {
			return false;
		}
		t_mac_move_log log = this.findOverVerifyingLog(mac.get_id());

		if ("不通过".equals(verify_flag)) {
			mac.setFlag("停用");
			mac.setFlag_stop("在库");

			log.setVerify_flag("不通过");
			log.setIs_end("是");
		} else if ("通过".equals(verify_flag)) {
			mac.setFlag_stop("上缴中");

			log.setVerify_flag("通过");
			log.setIs_end("否");
		}

		log.setVerify_note(note);

		log.setVerify_bank_id(WebTools.getCurrentBankId());
		log.setVerify_user_id(WebTools.getCurrentUserId());
		log.setVerify_time(new Timestamp(System.currentTimeMillis()));

		ht.update(mac);
		ht.update(log);

		return true;

	}

	// 查询调拨审批后的 日志
	public t_mac_move_log findOverVerifyedLog(String mac_id) {
		String hql = "from t_mac_move_log t where t.status=0 and t.mac_id=? and t.type='上缴' and t.verify_flag='通过' and t.is_end='否' and t.verify_bank_id=?";
		System.err.println("---------"+hql);
		t_mac_move_log log = (t_mac_move_log) super.findObj(hql, mac_id,
				WebTools.getCurrentBankId());

		return log;
	}

	// 上缴 接收
	@Transactional
	public boolean over_recv(String mac_id, String note) {

		t_mac mac = super.findObjById(mac_id, t_mac.class);

		if (!("停用".equals(mac.getFlag()) && mac.getFlag_stop().equals("上缴中"))) {
			return false;
		}

		mac.setFlag("停用");
		mac.setFlag_stop("在库");
		mac.setBank_id(WebTools.getCurrentBankId());

		t_mac_move_log transferVerifyedLog = findOverVerifyedLog(mac.get_id());
		transferVerifyedLog.setIs_end("是");

		t_mac_move_log log = new t_mac_move_log();
		log.setMac_id(mac.get_id());
		log.setType("上缴接收");
		log.setNote(note);
		log.setBank_id(WebTools.getCurrentBankId());
		log.setBank_id_dest(WebTools.getCurrentBankId());
		log.setUser_id(WebTools.getCurrentUserId());
		log.setIs_verify("否");
		log.setIs_end("是");

		ht.update(mac);
		ht.update(transferVerifyedLog);
		ht.save(log);

		return true;
	}
	
	public t_mac_move_log move(String mac_id,String type){
		
		if("分发".equals(type)){
			String hql="from t_mac_move_log t where t.mac_id=? and t.type=? and t.status=0 order by t.created desc limit 1";
			t_mac_move_log move = super.findObj(hql,mac_id,"分发");
			if(move==null){
				return null;
			}
			return move;
		}else if("接收".equals(type)){
			String hql="from t_mac_move_log t where t.mac_id=? and t.type=? and t.status=0 order by t.created desc limit 1";
			t_mac_move_log move = super.findObj(hql,mac_id,"接收");
			if(move==null){
				return null;
			}
			return move;
		}else{
			String hql="from t_mac_move_log t where t.mac_id=? and t.type=? and t.status=0 order by t.created desc limit 1";
			t_mac_move_log move = super.findObj(hql,mac_id,"调拨");
			if(move==null){
				return null;
			}
			return move;
		}
	}

	public static void main(String[] args) {
		MacMoveLogService s = SpringTools.getBean(MacMoveLogService.class);
		System.err.println(s);

	}

}
